package com.ruoyi.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.admin.domain.*;
import com.ruoyi.admin.mapper.ZhxBookMapper;
import com.ruoyi.admin.mapper.ZhxLikeJielongMapper;
import com.ruoyi.admin.mapper.ZhxSubscribeMapper;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.ZhxJielongMapper;
import com.ruoyi.admin.service.IZhxJielongService;

/**
 * 接龙管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class ZhxJielongServiceImpl implements IZhxJielongService 
{
    @Autowired
    private ZhxJielongMapper zhxJielongMapper;

    @Autowired
    private ZhxBookMapper zhxBookMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private ZhxLikeJielongMapper zhxLikeJielongMapper;

    @Autowired
    private ZhxSubscribeMapper zhxSubscribeMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询接龙管理
     * 
     * @param jielongId 接龙管理主键
     * @return 接龙管理
     */
    @Override
    public ZhxJielong selectZhxJielongByJielongId(Long jielongId)
    {
        return zhxJielongMapper.selectZhxJielongByJielongId(jielongId);
    }

    /**
     * 查询接龙管理列表
     * 
     * @param zhxJielong 接龙管理
     * @return 接龙管理
     */
    @Override
    public List<ZhxJielong> selectZhxJielongList(ZhxJielong zhxJielong)
    {
        return zhxJielongMapper.selectZhxJielongList(zhxJielong);
    }

    /**
     * 查询接龙列表
     * 根据点赞量(likeNum)从高到低排序
     * @param zhxJielong 接龙
     * @return 接龙
     */
    @Override
    public List<ZhxJielong> selectZhxJielongListByLike(ZhxJielong zhxJielong)
    {
        // 查询所有评论
        List<ZhxJielong> zhxJielongs = zhxJielongMapper.selectZhxJielongListByLike(zhxJielong);

        // 获取登录用户的id
        Long sysUserId = 0L;
        if (SecurityUtils.getUnUserId() != -1) {
            sysUserId = SecurityUtils.getUserId();
        }

        // 获取评论点赞信息
        Map<String, Long> zhxLikeJielongCache = new HashMap<>();
        if (redisCache.getCacheObject("zhxLikeJielongCache") != null) {
            zhxLikeJielongCache = redisCache.getCacheObject("zhxLikeJielongCache");
        } else {
            List<ZhxLikeJielong> zhxLikeJielongs = zhxLikeJielongMapper.selectZhxLikeJielongList(new ZhxLikeJielong());

            for (ZhxLikeJielong zhxLikeJielong : zhxLikeJielongs) {
                zhxLikeJielongCache.put(zhxLikeJielong.getUserId() + "-" + zhxLikeJielong.getJielongId(),1L);
            }
            redisCache.setCacheObject("zhxLikeJielongCache", zhxLikeJielongCache);
        }

        // 遍历所有评论，将其按照父子关系分组，并设置用户信息
        for (ZhxJielong jielong : zhxJielongs) {
            // 从缓存中获取用户信息，如果没有则从数据库中查询并存入缓存
            User user = redisCache.getCacheObject("user:" + jielong.getUserId());;
            if (user == null) {
                user = userMapper.selectUserByIdEasy(jielong.getUserId());
                redisCache.setCacheObject("user:" + user.getUserId(), user);
            }
            jielong.setSysUser(user);
            jielong.setIsLike(zhxLikeJielongCache.get(sysUserId + "-" + jielong.getJielongId()) == null ? 0 : 1);
        }


        return zhxJielongs;
    }

    /**
     * 新增接龙管理
     * 
     * @param zhxJielong 接龙管理
     * @return 结果
     */
    @Override
    public int insertZhxJielong(ZhxJielong zhxJielong)
    {
        zhxJielong.setCreateTime(DateUtils.getNowDate());
        return zhxJielongMapper.insertZhxJielong(zhxJielong);
    }

    /**
     * 新增接龙管理(移动端)
     * 增加book/jielong接龙数量
     *
     * @param zhxJielong 接龙管理
     * @return 结果
     */
    @Override
    public int insertZhxJielongTodo(ZhxJielong zhxJielong)
    {
        zhxJielong.setUserId(SecurityUtils.getUserId());
        zhxJielong.setCreateTime(DateUtils.getNowDate());

        // 如果是作品接龙，增加作品接龙数
        // 接龙主体类型为1（作品）
        if (zhxJielong.getJielongParentType().equals("1")) {
            ZhxBook zhxBook = zhxBookMapper.selectZhxBookByBookId(zhxJielong.getJielongParentId());
            zhxBook.setJielongNum(zhxBook.getJielongNum() + 1);
            zhxBookMapper.updateZhxBook(zhxBook);
            zhxJielong.setJielongLevel(1L);

            // 订阅新增数量+1
            ZhxSubscribe zhxSubscribe = new ZhxSubscribe();
            zhxSubscribe.setSubscribeType("1");
            zhxSubscribe.setBookId(zhxJielong.getJielongParentId());
            int i = zhxSubscribeMapper.addNewQuantity(zhxSubscribe);
        }

        // 如果是接龙接龙，增加接龙接龙数
        // 接龙主体类型为2（接龙）
        if (zhxJielong.getJielongParentType().equals("2")) {
            // 查询父级接龙
            ZhxJielong zhxJielong0 = zhxJielongMapper.selectZhxJielongByJielongId(zhxJielong.getJielongParentId());
            // 父级接龙接龙数+1
            zhxJielong0.setJielongNum(zhxJielong.getJielongNum() + 1);
            zhxJielongMapper.updateZhxJielong(zhxJielong0);

            // 订阅新增数量+1
            ZhxSubscribe zhxSubscribe = new ZhxSubscribe();
            zhxSubscribe.setSubscribeType("2");
            zhxSubscribe.setBookId(zhxJielong.getJielongParentId());
            int i = zhxSubscribeMapper.addNewQuantity(zhxSubscribe);

            // 查询当前接龙创建用户
//            User user = userMapper.selectUserByIdEasy(zhxJielong.getUserId());
            User user = redisCache.getCacheObject("user:" + zhxJielong.getUserId());
            if (user == null) {
                user = userMapper.selectUserByIdEasy(zhxJielong.getUserId());
            }
            // 当前接龙创建用户发布接龙数+1
            user.setSetJielongNum(user.getSetJielongNum() + 1);

            // 判断是否为自己的接龙创建接龙
            if (!zhxJielong0.getUserId().equals(zhxJielong.getUserId())) {
                // 查询父级接龙创建用户
//                User user0 = userMapper.selectUserByIdEasy(zhxJielong0.getUserId());
                User user0 = redisCache.getCacheObject("user:" + zhxJielong.getUserId());
                if (user0 == null) {
                    user0 = userMapper.selectUserByIdEasy(zhxJielong.getUserId());
                }
                // 父级接龙创建用户收到后续数+1
                user0.setGetJielongNum(user.getGetJielongNum() + 1);
                userMapper.updateUserEasy(user0);
                redisCache.setCacheObject("user:" + user0.getUserId(), user0);
            }
            userMapper.updateUserEasy(user);
            redisCache.setCacheObject("user:" + user.getUserId(), user);

            // 当前接龙层级数+1
            zhxJielong.setJielongLevel(zhxJielong0.getJielongLevel() + 1);
        }

        // 接龙主体作品人气值加10
        ZhxBook zhxBook1 = zhxBookMapper.selectZhxBookByBookId(zhxJielong.getJielongBodyId());
        zhxBook1.setPopularity(zhxBook1.getPopularity() + 10);
        zhxBookMapper.updateZhxBook(zhxBook1);

        int i = zhxJielongMapper.insertZhxJielong(zhxJielong);

        return i;
    }

    /**
     * 修改接龙管理
     * 
     * @param zhxJielong 接龙管理
     * @return 结果
     */
    @Override
    public int updateZhxJielong(ZhxJielong zhxJielong)
    {
        zhxJielong.setUpdateTime(DateUtils.getNowDate());
        return zhxJielongMapper.updateZhxJielong(zhxJielong);
    }

    /**
     * 批量删除接龙管理
     * 
     * @param jielongIds 需要删除的接龙管理主键
     * @return 结果
     */
    @Override
    public int deleteZhxJielongByJielongIds(Long[] jielongIds)
    {
        return zhxJielongMapper.deleteZhxJielongByJielongIds(jielongIds);
    }

    /**
     * 删除接龙管理信息
     * 
     * @param jielongId 接龙管理主键
     * @return 结果
     */
    @Override
    public int deleteZhxJielongByJielongId(Long jielongId)
    {
        return zhxJielongMapper.deleteZhxJielongByJielongId(jielongId);
    }

    /**
     * 获取接龙数量
     */
    @Override
    public int getJielongNum() {
        return zhxJielongMapper.getJielongNum();
    }

    /**
     * 获取今日新增接龙数量
     */
    @Override
    public int getJielongNumToday() {
        return zhxJielongMapper.getJielongNumToday();
    }
    
}
